package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

public class UpdateWorkingHoursOfEmployee {
	private Vector<String> totalTodayWorkHours;
	Vector<String> empHours;
	int status;
    
	
	public UpdateWorkingHoursOfEmployee(Vector<String> userHours)
	{
		this.empHours=userHours;
		this.totalTodayWorkHours = new Vector<String>();
	}
	
	
	
	public void saveNewHours()
	{
		SQLConnection sc=new SQLConnection();
		if(Integer.parseInt(empHours.get(4))==0)
		{
		try {
			
			PreparedStatement ps1 = sc.getConnection().prepareStatement("INSERT INTO employee_working_hours(Eid,date,date_out,clock_in,clock_out) values(?,?,?,?,?);");	
			 
			sc.getConnection().setAutoCommit(false);
		
			  ps1.setString(1,empHours.get(0));
			  ps1.setString(2,empHours.get(1));
			  ps1.setString(3,empHours.get(1));
			  ps1.setString(4,empHours.get(2));
			  ps1.setString(5,empHours.get(3));
			  
			
			ps1.executeUpdate();
			sc.getConnection().commit();
			ps1.close();
			status=1;
		}
		
		catch (SQLException e) {
			
			e.printStackTrace();
			status=0;
		}
		}
		else {
			
			try {
				//PreparedStatement ps3 = sc.getConnection().prepareStatement("Delete FROM employee_working_hours Where  Eid=? and date=? ;");	
				PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE employee_working_hours SET clock_in=?, clock_out=? Where Eid=? and date=? and clock_in=?"); // FROM employee_working_hours       //("UPDATE employee_working_hours set clock_in=?, clock_out=?,in_hours=?,in_minuts=?,in_seconds=?,out_hours=?,out_minuts=?,out_seconds=? Where Eid=? and date=?");   //("INSERT INTO employee_working_hours (Eid, date, clock_in,clock_out,in_hours,in_minuts,in_seconds,out_hours,out_minuts,out_seconds) values(?,?,?,?,?,?,?,?,?,?) ;");	
				 
				sc.getConnection().setAutoCommit(false);
			
				  ps2.setString(3,empHours.get(0));
				  ps2.setString(4,empHours.get(1));				 
				  ps2.setString(1,empHours.get(2));           
				  ps2.setString(2,empHours.get(3));          
				  ps2.setString(5,empHours.get(5));
				  
				ps2.executeUpdate();
				sc.getConnection().commit();
				ps2.close();
				sc.getConnection().close();
				status=1;
			}
			
			catch (SQLException e) {
				
				e.printStackTrace();
				status=0;
			}
			
		}
		
	}
	
	
	
	public void calculateHours()
	{
		 	
		SQLConnection sc1=new SQLConnection();
		
		try {
			PreparedStatement ps1 = sc1.getConnection().prepareStatement("SELECT clock_in,clock_out FROM employee_working_hours ewh Where Eid=? and date=?;");   //("SELECT out_hours,in_hours,out_minuts,in_minuts FROM employee_working_hours ewh Where Eid=? and date=?;");    // dobavit = work_id
			ps1.setString(1,(String) empHours.get(0));
			ps1.setString(2,(String) empHours.get(1));
			 
			ResultSet rs1=ps1.executeQuery();
			ResultSetMetaData rsmd = rs1.getMetaData();
			
			//totalTodayWorkHours = new Vector<String>();
					while(rs1.next())
		        {  
				 
				 for(int col = 1; col <= rsmd.getColumnCount(); col++) 
		         	{                      	                
				    						
					 int type = rsmd.getColumnType(col);
		                switch(type)
		                {
		                
		                    case Types.INTEGER :
		                    	totalTodayWorkHours.add(new Integer(rs1.getInt(col)).toString());
		                        break;
		                        
		                    case Types.VARCHAR :
		                    	totalTodayWorkHours.add(rs1.getString(col));
		                        break;
		                    case Types.DATE :
		                    	totalTodayWorkHours.add(rs1.getDate(col).toString());
		                    	
		                        break;   
		                        
		                    case Types.TIME :      
			                   	 
		                    	 if(rs1.getTime(col)!=null)
		                         { 
		                     
		                    		 totalTodayWorkHours.add(rs1.getTime(col).toString());
		                    	 
		                    	 }
		                    	 else  totalTodayWorkHours.add("empty");
		                     break;  
				
				
		                }
		               
		         	}
				
				 
						        }
					 
			 ps1.close();
			 rs1.close();	
			 
			 
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 
			e.printStackTrace();
		}	
		
	}
	

	public Vector<String> getTotalTodayWorkHours() {
return totalTodayWorkHours;
}


    public void setTotalTodayWorkHours(Vector<String> totalWorkHours) {
this.totalTodayWorkHours = totalWorkHours;
}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	
}



